﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;
using Microsoft.AspNet.Identity;

namespace Yunwei.Web.Areas.Admin.Controllers
{
    public class RestaurantController : Controller
    {
        IRestaurantService _restarantService;
        IUserService _userService;
        public RestaurantController()
        {
            _userService = new UserService();
            _restarantService = new RestaurantService();
        }


        public ActionResult List()
        {
            if (Request.IsAuthenticated)
            {
                var restaurants = _restarantService.FindList(10, "", User.Identity.GetUserId<int>(), 1);

                return View(restaurants);
            }

            return RedirectToAction("Login", "User", new { Area = "Admin", returnUrl = "~/Admin/Restaurant/List" });
        }


        public ActionResult Add()
        {
            return View(new Restaurant());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Add(Restaurant restaurant, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                restaurant.User_Id = User.Identity.GetUserId<int>();
                //ViewBag.returnURL = returnUrl;
                if (_restarantService.Add(restaurant) != null)
                {
                    return RedirectToAction("List", "Restaurant");
                }
                else
                {
                    ModelState.AddModelError("Name", "添加餐馆失败");
                }
            }

            return View();
        }

        public ActionResult Details(int id)
        {
            if (Request.IsAuthenticated)
            {
                Restaurant restaurant = _restarantService.Find(id);
                if (restaurant != null)
                    return View(restaurant);
                else
                    return RedirectToAction("Error404", "Home", new { returnUrl = "/Admin/Restaurant/List" });
            }

            return RedirectToAction("Error404", "Home",new { returnUrl = "/Admin/Restaurant/List" });
        }


        public ActionResult Edit(int id)
        {
            if (Request.IsAuthenticated)
            {
                Restaurant restaurant = _restarantService.Find(id);
                if (restaurant != null)
                    return View(restaurant);
                else
                    return RedirectToAction("Error404", "Home", new { returnUrl = "/Admin/Restaurant/List" });
            }

            return RedirectToAction("Error404", "Home", new { returnUrl = "/Admin/Restaurant/List" });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(Restaurant restaurant, string returnUrl)
        {
            if (Request.IsAuthenticated)
            {
                if (restaurant != null)
                {
                    restaurant.User_Id = User.Identity.GetUserId<int>();
                    if (_restarantService.Update(restaurant))
                    {
                        ModelState.AddModelError("","更新成功");
                    }
                    else
                    {
                        ModelState.AddModelError("", "更新失败");
                    }

                    return View(restaurant);
                }
                else
                    return RedirectToAction("Error404", "Home", new { returnUrl = "/Admin/Restaurant/List" });
            }

            return RedirectToAction("Error404", "Home", new { returnUrl = "/Admin/Restaurant/List" });
        }

        public ActionResult Delete(int id, string returnUrl)
        {
            if (Request.IsAuthenticated)
            {
                _restarantService.Delete(_restarantService.Find(id));
            }

            return RedirectToAction("List", "Restaurant");
        }
    }
}